package org.come.Jpanel;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.*;

import com.updateNew.MyIsif;
import org.come.Frame.TestSetupJframe;
import org.come.bean.OneArenaNotes;
import org.come.bean.OneArenaRole;
import org.come.until.*;

import com.tool.btn.PartnerArenaBtn;
import com.tool.tcpimg.UIUtils;



/**
 * @author HGC<br>
 * @time 2020年3月19日 下午3:13:19<br>
 * @class 类名:PartnerArenaModelPanel 伙伴竞技排行榜model <br>
 */
public class PartnerArenaModelPanel extends JPanel implements MouseListener {
    private JLabel labImg;
    private JLabel labNum;
    private JLabel labName;
    private JLabel labLvl;
    private JLabel labUpDown;
    private JLabel labUpDownNum;
    private JLabel labTime;
    private PartnerArenaBtn btnChallenge;
    private OneArenaRole oneArenaRole;
    private int type;
    SimpleDateFormat formatter;
    private ImageIcon icon;
    private ImageIcon iconBack;

    public PartnerArenaModelPanel() {
        this.formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setPreferredSize(new Dimension(334, 49));
        this.setLayout(null);
        this.setOpaque(false);
        this.setBackground(Color.gray);
        this.type = 0;
        this.getLabImg();
        this.getLabLvl();
        this.getLabName();
        this.getLabNum();
        this.getBtnChallenge();
        this.addMouseListener((MouseListener)this);
    }

    public PartnerArenaModelPanel(final int type) {
        this.formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setPreferredSize(new Dimension(343, 41));
        this.setLayout(null);
        this.setOpaque(false);
        this.setBackground(Color.gray);
        this.type = type;
        this.getLabImgType();
        this.getLabLvlType();
        this.getLabNameType();
        this.getLabNumType();
        this.getLabUpDown();
        this.getLabUpDownNum();
        this.getLabTime();
        this.addMouseListener((MouseListener)this);
    }

    /** 展示挑战面板 */
    public void showView(final OneArenaRole oneArenaRole, final int type) {
        this.oneArenaRole = oneArenaRole;
        final int lvl = oneArenaRole.getLvl();
        this.labLvl.setText(AnalysisString.lvl(lvl) + "级");
        final int lvltrue = AnalysisString.lvltrue(lvl);
        this.labLvl.setForeground(UIUtils.getcolor(lvltrue));
        this.labName.setText(oneArenaRole.getName());
        final int place = oneArenaRole.getPlace();
        this.labNum.setText(String.valueOf(place));
        this.labImg.setIcon((Icon)CutButtonImage.getImage("img/head/" + oneArenaRole.getSkin() + ".png", 39, 39));
        if (type != 0) {
            if (type == -1 && place <= 5) {
                this.btnChallenge.setVisible(false);
            }
            else {
                this.btnChallenge.setVisible(true);
            }
        }
    }

    /** 挑战记录面板展示 */
    public void showWarView(final OneArenaNotes arenaNotes, final BigDecimal roleId) {
        if (arenaNotes.getRole1().compareTo(roleId) == 0) {
            this.refreshView(arenaNotes.getLvl2(), arenaNotes.getName2(), (arenaNotes.getIsV() == 0) ? 1 : 2, arenaNotes.getPlace(), arenaNotes.getTime(), arenaNotes.getSkin2());
        }
        else {
            if (arenaNotes.getRole2().compareTo(roleId) == 0) {
                this.refreshView(arenaNotes.getLvl1(), arenaNotes.getName1(), (arenaNotes.getIsV() == 0) ? 2 : 1, arenaNotes.getPlace(), arenaNotes.getTime(), arenaNotes.getSkin1());
            }
        }
    }

    public void refreshView(final int lvl, final String name, final int nextInt, final int place, final String time, final String skin) {
        this.labLvl.setText(AnalysisString.lvl(lvl) + "级");
        final int lvltrue = AnalysisString.lvltrue(lvl);
        this.labLvl.setForeground(UIUtils.getcolor(lvltrue));
        this.labName.setText(name);
        if (nextInt == 1) {
            this.labNum.setForeground(Color.green);
            this.labNum.setText("胜");
            this.labUpDown.setIcon(new ImageIcon("inkImg/number/npk/b/S183.png"));
            this.labUpDownNum.setText("+" + place);
            this.labUpDownNum.setForeground(Color.green);
        }
        else {
            this.labNum.setForeground(Color.red);
            this.labNum.setText("负");
            this.labUpDown.setIcon(new ImageIcon("inkImg/number/npk/b/S184.png"));
            this.labUpDownNum.setText("-" + place);
            this.labUpDownNum.setForeground(Color.red);
        }
        this.labTime.setText(this.getTimes(time));
        this.labImg.setIcon((Icon)CutButtonImage.getImage("img/head/" + skin + ".png", 39, 39));
    }

    public String getTimes(final String str) {
        String resultTimes = "";
        long date = 0L;
        try {
            date = this.formatter.parse(str).getTime();
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        final long timeNow = Util.getTime();
        final long times = timeNow - date;
        final long day = times / 86400000L;
        final long hour = times / 3600000L - day * 24L;
        final long min = times / 60000L - day * 24L * 60L - hour * 60L;
        final StringBuffer sb = new StringBuffer();
        if (day > 0L) {
            sb.append(day);
            sb.append("天前");
        }
        else {
            if (hour > 0L) {
                sb.append(hour);
                sb.append("小时前");
            }
            else {
                if (min > 0L) {
                    sb.append(min);
                    sb.append("分钟前");
                }
                else {
                    sb.append("刚刚");
                }
            }
        }
        resultTimes = sb.toString();
        return resultTimes;
    }

    @Override
    protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        try {
            if("水墨UI".equals(MyIsif.getStyle())) {
                this.btnChallenge.setIcons(CutButtonImage.cuts("skin/8100226/newbutton/B30.png"));//挑战按键图片
                this.btnChallenge.setBounds(180, 30, 34, 17);
                this.btnChallenge.setColors(UIUtils.COLOR_BTNTEXT);
            }else{
                this.btnChallenge.setIcons(CutButtonImage.cuts("inkImg/hongmu/1/ss531.png"));//挑战按键图片
                this.btnChallenge.setBounds(180, 30, 34, 17);
                this.btnChallenge.setColors(UIUtils.COLOR_BTNPUTONG2);
            }

        }
        catch (Exception ex) {}
        if (this.icon == null) {
            this.icon = CutButtonImage.getImage("inkImg/old/background/10.png", -1, -1);
        }
        if (this.type == 0) {
            if (this.iconBack == null) {
                this.iconBack = CutButtonImage.getImage("inkImg/old/1/S108.png", -1, -1);//挑战下划线图片
            }
            g.drawImage(this.icon.getImage(), 37, 5, 39, 39, null);
            g.drawImage(this.iconBack.getImage(), 15, 48, 240, 1, null);
        }
        else {
            if (this.type == 1) {
                if (this.iconBack == null) {
                    this.iconBack = CutButtonImage.getImage("inkImg/old/background/S177.png", -1, -1);//战报下划线图片
                }
                g.drawImage(this.icon.getImage(), 90, 1, 39, 39, null);
                g.drawImage(this.iconBack.getImage(), 0, 40, 345, 1, null);
            }
        }
    }

    public JLabel getLabImg() {
        if (this.labImg == null) {
            (this.labImg = new JLabel()).setBounds(38, 6, 37, 37);
            this.labImg.setOpaque(false);
            this.add((Component)this.labImg);
        }
        return this.labImg;
    }

    public void setLabImg(final JLabel labImg) {
        this.labImg = labImg;
    }

    public JLabel getLabNum() {
        if (this.labNum == null) {
            (this.labNum = new JLabel("", 0)).setForeground(Color.yellow);
            this.labNum.setFont(UIUtils.TEXT_FONT);
            this.labNum.setBounds(0, 0, 37, 49);
            this.labNum.setOpaque(false);
            this.add((Component)this.labNum);
        }
        return this.labNum;
    }

    public void setLabNum(final JLabel labNum) {
        this.labNum = labNum;
    }

    public JLabel getLabName() {
        if (this.labName == null) {
            (this.labName = new JLabel()).setBounds(122, 4, 100, 20);//挑战敌方名字
            this.labName.setForeground(Color.white);
            this.labName.setFont(UIUtils.TEXT_FONT1);
            this.labName.setOpaque(false);
            this.add((Component)this.labName);
        }
        return this.labName;
    }

    public void setLabName(final JLabel labName) {
        this.labName = labName;
    }

    public JLabel getLabLvl() {
        if (this.labLvl == null) {
            (this.labLvl = new JLabel()).setBounds(122, 28, 70, 20);//挑战敌方等级
            this.labLvl.setOpaque(false);
            this.labLvl.setFont(UIUtils.TEXT_FONT1);
            this.add((Component)this.labLvl);
        }
        return this.labLvl;
    }

    public void setLabLvl(final JLabel labLvl) {
        this.labLvl = labLvl;
    }

    public ImageIcon getIcon() {
        return this.icon;
    }

    public void setIcon(final ImageIcon icon) {
        this.icon = icon;
    }

    public PartnerArenaBtn getBtnChallenge() {
        if (this.btnChallenge == null) {
            (this.btnChallenge = new PartnerArenaBtn("", 1, UIUtils.danxin, "挑战", UIUtils.TEXT_FONT, 3, this)).setBounds(290, 20, 34, 17);
            this.add((Component)this.btnChallenge);
        }
        return this.btnChallenge;
    }

    public void setBtnChallenge(final PartnerArenaBtn btnChallenge) {
        this.btnChallenge = btnChallenge;
    }

    @Override
    public void mouseClicked(final MouseEvent e) {
    }

    @Override
    public void mousePressed(final MouseEvent e) {
    }

    @Override
    public void mouseReleased(final MouseEvent e) {
    }

    @Override
    public void mouseEntered(final MouseEvent e) {
        this.setOpaque(true);
    }

    @Override
    public void mouseExited(final MouseEvent e) {
        MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
        this.setOpaque(false);
    }

    public OneArenaRole getOneArenaRole() {
        return this.oneArenaRole;
    }

    public void setOneArenaRole(final OneArenaRole oneArenaRole) {
        this.oneArenaRole = oneArenaRole;
    }

    public ImageIcon getIconBack() {
        return this.iconBack;
    }

    public void setIconBack(final ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public JLabel getLabLvlType() {
        if (this.labLvl == null) {
            (this.labLvl = new JLabel()).setBounds(130, 21, 70, 20);
            this.labLvl.setOpaque(false);
            this.labLvl.setFont(UIUtils.TEXT_FONT1);
            this.add((Component)this.labLvl);
        }
        return this.labLvl;
    }

    public JLabel getLabNameType() {
        if (this.labName == null) {
            (this.labName = new JLabel()).setBounds(130, 1, 120, 20);
            this.labName.setForeground(Color.white);
            this.labName.setFont(UIUtils.TEXT_FONT1);
            this.labName.setOpaque(false);
            this.add((Component)this.labName);
        }
        return this.labName;
    }

    public JLabel getLabImgType() {
        if (this.labImg == null) {
            (this.labImg = new JLabel()).setBounds(90, 1, 37, 37);
            this.labImg.setOpaque(false);
            this.add((Component)this.labImg);
        }
        return this.labImg;
    }

    public JLabel getLabNumType() {
        if (this.labNum == null) {
            (this.labNum = new JLabel("", 0)).setFont(UIUtils.TEXT_RESULT);
            this.labNum.setBounds(0, 0, 49, 49);
            this.labNum.setOpaque(false);
            this.add((Component)this.labNum);
        }
        return this.labNum;
    }

    public JLabel getLabUpDown() {
        if (this.labUpDown == null) {
            (this.labUpDown = new JLabel()).setBounds(60, 1, 19, 24);
            this.add((Component)this.labUpDown);
        }
        return this.labUpDown;
    }

    public void setLabUpDown(final JLabel labUpDown) {
        this.labUpDown = labUpDown;
    }

    public JLabel getLabUpDownNum() {
        if (this.labUpDownNum == null) {
            (this.labUpDownNum = new JLabel("", 0)).setFont(UIUtils.TEXT_FONT);
            this.labUpDownNum.setBounds(50, 25, 40, 16);
            this.add((Component)this.labUpDownNum);
        }
        return this.labUpDownNum;
    }

    public void setLabUpDownNum(final JLabel labUpDownNum) {
        this.labUpDownNum = labUpDownNum;
    }

    public int getType() {
        return this.type;
    }

    public void setType(final int type) {
        this.type = type;
    }

    public JLabel getLabTime() {
        if (this.labTime == null) {
            (this.labTime = new JLabel()).setBounds(250, 10, 90, 20);
            this.labTime.setForeground(Color.white);
            this.labTime.setFont(UIUtils.TEXT_FONT);
            this.add((Component)this.labTime);
        }
        return this.labTime;
    }

    public void setLabTime(final JLabel labTime) {
        this.labTime = labTime;
    }
}
